package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.dao.cookie.CommonUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.AttentionDao;

public class AttentionAdd extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int leagueId = Integer.valueOf(request.getParameter("league"));
		try {
			CommonUserSessionCookieAccess coSessionCookieAccess =
					new CommonUserSessionCookieAccess(request);
			int userId = coSessionCookieAccess.getUserId();
			AttentionDao attentionDao = AttentionDao.getDao();
			attentionDao.addAttention(leagueId, userId);
			response.getWriter().print("success");
		} catch (SessionIdNotFound e) {
			response.getWriter().print("failure");
			return;
		} catch (SQLException e) {
			response.getWriter().print("failure");
			return;
		}
	}

}
